package com.nk.system.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.log4j.Logger;


public class CharacterEncodingFilter implements Filter {
	private Logger log = Logger.getLogger(CharacterEncodingFilter.class);

	private String encoding = "utf-8";

	public void doFilter(ServletRequest request,ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
		
		request.setCharacterEncoding(encoding);
		response.setCharacterEncoding(encoding);
		
		filterChain.doFilter(request, response);
//		try {
//			filterChain.doFilter(request, response);
//		}catch(Exception ex) {
//			log.info(ex,ex);
//		}
	}

	public void init(FilterConfig filterConfig)

	throws ServletException {
		String encodingParam = filterConfig.getInitParameter("encoding");
		if (encodingParam != null) {
			encoding = encodingParam;
		}
	}

	public void destroy() {
		// nothing todo
	}
	
//    public void doFilter(ServletRequest request, ServletResponse response,
//        FilterChain next) throws IOException, ServletException 
//	{
//    	log.info("#Start doFilter");
//	    String encoding = request.getCharacterEncoding();
//	    if (encoding == null || encoding.length() == 0)
//	    {
//	        request.setCharacterEncoding("UTF-8");
//	    }
//	    encoding = request.getCharacterEncoding();
//	    log.info("# encoding = "+encoding);
//	    next.doFilter(request, response); 
//	}
//
//	public void destroy() {
//		// TODO Auto-generated method stub
//		
//	}
//
//	public void init(FilterConfig arg0) throws ServletException {
//		// TODO Auto-generated method stub
//		
//	}
	
}

